<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="单位名称" prop="companyId">
      <el-input v-model="dataForm.companyId" placeholder="单位名称"></el-input>
    </el-form-item>
    <el-form-item label="机房名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="机房名称"></el-input>
    </el-form-item>
    <el-form-item label="机房地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="机房地址"></el-input>
    </el-form-item>
    <el-form-item label="机房建设" prop="construction">
      <el-input v-model="dataForm.construction" placeholder="机房建设"></el-input>
    </el-form-item>
    <el-form-item label="是否屏蔽机房" prop="screen">
      <el-input v-model="dataForm.screen" placeholder="是否屏蔽机房"></el-input>
    </el-form-item>
    <el-form-item label="面积" prop="area">
      <el-input v-model="dataForm.area" placeholder="面积"></el-input>
    </el-form-item>
    <el-form-item label="使用时间" prop="useTime">
      <el-input v-model="dataForm.useTime" placeholder="使用时间"></el-input>
    </el-form-item>
    <el-form-item label="维护方式" prop="maintenanceMode">
      <el-input v-model="dataForm.maintenanceMode" placeholder="维护方式"></el-input>
    </el-form-item>
    <el-form-item label="机柜总数" prop="cabinetTotal">
      <el-input v-model="dataForm.cabinetTotal" placeholder="机柜总数"></el-input>
    </el-form-item>
    <el-form-item label="机柜使用数" prop="cabinetUsed">
      <el-input v-model="dataForm.cabinetUsed" placeholder="机柜使用数"></el-input>
    </el-form-item>
    <el-form-item label="机房可增加机柜数" prop="cabinetIncrease">
      <el-input v-model="dataForm.cabinetIncrease" placeholder="机房可增加机柜数"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          companyId: '',
          name: '',
          address: '',
          construction: '',
          screen: '',
          area: '',
          useTime: '',
          maintenanceMode: '',
          cabinetTotal: '',
          cabinetUsed: '',
          cabinetIncrease: ''
        },
        dataRule: {
          companyId: [
            { required: true, message: '单位名称不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '机房名称不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '机房地址不能为空', trigger: 'blur' }
          ],
          construction: [
            { required: true, message: '机房建设不能为空', trigger: 'blur' }
          ],
          screen: [
            { required: true, message: '是否屏蔽机房不能为空', trigger: 'blur' }
          ],
          area: [
            { required: true, message: '面积不能为空', trigger: 'blur' }
          ],
          useTime: [
            { required: true, message: '使用时间不能为空', trigger: 'blur' }
          ],
          maintenanceMode: [
            { required: true, message: '维护方式不能为空', trigger: 'blur' }
          ],
          cabinetTotal: [
            { required: true, message: '机柜总数不能为空', trigger: 'blur' }
          ],
          cabinetUsed: [
            { required: true, message: '机柜使用数不能为空', trigger: 'blur' }
          ],
          cabinetIncrease: [
            { required: true, message: '机房可增加机柜数不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/aucomputerroom/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.companyId = data.aucomputerroom.companyId
                this.dataForm.name = data.aucomputerroom.name
                this.dataForm.address = data.aucomputerroom.address
                this.dataForm.construction = data.aucomputerroom.construction
                this.dataForm.screen = data.aucomputerroom.screen
                this.dataForm.area = data.aucomputerroom.area
                this.dataForm.useTime = data.aucomputerroom.useTime
                this.dataForm.maintenanceMode = data.aucomputerroom.maintenanceMode
                this.dataForm.cabinetTotal = data.aucomputerroom.cabinetTotal
                this.dataForm.cabinetUsed = data.aucomputerroom.cabinetUsed
                this.dataForm.cabinetIncrease = data.aucomputerroom.cabinetIncrease
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/aucomputerroom/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'companyId': this.dataForm.companyId,
                'name': this.dataForm.name,
                'address': this.dataForm.address,
                'construction': this.dataForm.construction,
                'screen': this.dataForm.screen,
                'area': this.dataForm.area,
                'useTime': this.dataForm.useTime,
                'maintenanceMode': this.dataForm.maintenanceMode,
                'cabinetTotal': this.dataForm.cabinetTotal,
                'cabinetUsed': this.dataForm.cabinetUsed,
                'cabinetIncrease': this.dataForm.cabinetIncrease
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
